package com.zlll.winner.service.bbs;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlll.winner.business.model.bbs.BbsPost;
import com.zlll.winner.business.model.bbs.BbsUserComment;
import com.zlll.winner.business.service.bbs.IBbsPostService;
import com.zlll.winner.business.service.bbs.IBbsUserActionLogService;
import com.zlll.winner.business.vo.bbs.BbsUserCommentVo;
import com.zlll.winner.enums.CommonEnum;
import com.zlll.winner.enums.bbs.ActionLogEnum;
import com.zlll.winner.enums.bbs.CommentEnum;
import com.zlll.winner.mapper.bbs.BbsUserCommentMapper;
import com.zlll.winner.business.service.bbs.IBbsUserCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlll.winner.service.bbs.async.BbsPostAsyncService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 论坛用户评论表 服务实现类
 * </p>
 *
 * @author Walert
 * @since 2019-09-12
 */
@Service
public class BbsUserCommentServiceImpl extends ServiceImpl<BbsUserCommentMapper, BbsUserComment> implements IBbsUserCommentService {

    @Autowired
    private BbsPostAsyncService postAsyncService;

    @Autowired
    private IBbsUserActionLogService actionLogService;

    @Autowired
    private IBbsPostService postService;

    /**
     * 论坛帖子用户评论分页列表
     *
     * @param params
     * @return
     */
    @Override
    public Object fetchCommentPage(JSONObject params) {
        Integer pageNo = params.getInteger("pageNo");
        Integer pageSize = params.getInteger("pageSize");

        IPage<BbsUserCommentVo> page = new Page<>(pageNo, pageSize);
        Long postId = params.getLong("postId");

        BbsPost post = postService.getById(postId);

        QueryWrapper<BbsUserCommentVo>  queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("t.biz_id",postId);
        queryWrapper.eq("t.biz_type", CommentEnum.BizTypeEnum.POST.val());

        String content = params.getString("content");
        if(StringUtils.isNotBlank(content)){
            queryWrapper.like("t.content",content);
        }
        queryWrapper.orderBy(false,false,"t.create_time");
        Long userId = params.getLong("userId");
        if(userId == null) userId = -1L;
        if(post!=null && post.getPosition() !=null && post.getPosition().equals("9")){
            //维权管理 是根据用户标识 进行排序的
            return baseMapper.selectPageByCustomerGuard(page,queryWrapper,userId);
        }else{
            return baseMapper.selectPageByCustomer(page,queryWrapper,userId);
        }

    }

    /**
     * 删除评论
     *
     * @param params
     * @return
     */
    @Override
    public Object removeComment(JSONObject params) throws InterruptedException {
        Long postId = params.getLong("postId");
        Long commentId = params.getLong("commentId");

        BbsUserComment bbsUserComment = new BbsUserComment();
        bbsUserComment.setId(commentId);
        bbsUserComment.setUpdateTime(System.currentTimeMillis());
        bbsUserComment.setIsDel(CommonEnum.DelEnum.DEL.val());
        int i = baseMapper.updateById(bbsUserComment);


        params.put("bizId",commentId);
        params.put("bizType",ActionLogEnum.BizTypeEnum.COMMENT.val());
        params.put("actionType", ActionLogEnum.ActionTypeEnum.REMOVE.val());
        Object o = actionLogService.saveLog(params);

        params.put("bizId",postId);
        params.put("bizType",ActionLogEnum.BizTypeEnum.POST.val());
        params.put("actionType", ActionLogEnum.ActionTypeEnum.REMOVE_COMMENT.val());
        Object o1 = actionLogService.saveLog(params);

        return bbsUserComment;
    }

    /**
     * 新增或更新评论
     *
     * @param params
     * @return
     */
    @Override
    public Object saveOrUpdateComment(BbsUserComment params) {
        if(params.getId()!=null){
            baseMapper.updateById(params);
        }else{
            baseMapper.insert(params);
        }
        Long userId = params.getUserId();
        if(userId == null) userId = -1L;
        return selectByCommentId(params.getId(),userId);
    }

    /**
     * 根据评论ID获取评论详情
     *
     * @param commentId
     * @param userId
     * @return
     */
    @Override
    public BbsUserCommentVo selectByCommentId(Long commentId, Long userId) {
        return baseMapper.selectByCommentId(commentId,userId);
    }

    /**
     * 条件获取用户评论列表
     * @param params
     * @return
     */
    public List<BbsUserCommentVo> getDetailsList(JSONObject params){
        QueryWrapper<BbsUserCommentVo> queryWrapper = new QueryWrapper<>();
        JSONArray commentIds = params.getJSONArray("commentIds");
        if(commentIds!= null && commentIds.size() > 0){
            queryWrapper.in("t.id",commentIds);
        }
        Long userId = params.getLong("userId");
        if(userId==null) userId = -1L;
        return baseMapper.getDetailsList(queryWrapper,userId);
    }
}
